#include "nr_core.h"

nr_device_property::nr_device_property()
{
}

nr_device_property::nr_device_property(nr_device_property& prop)
{
    copy_data(prop);
}

nr_device_property::~nr_device_property()
{
}

nr_device_property& nr_device_property::operator = (nr_device_property &prop)
{
    // Copy it 
    copy_data(prop);
    return *this;
}

nr_device_property::property_type_t nr_device_property::parse_property_type( const QString &type_str )
{
    const QString str_property_types[] =  {"Invalid",
                                           "PropertyGroup",
                                           "Property"};

    for(int i = 0; i < NR_ARRAY_SIZE(str_property_types); ++i)
    {
        if (type_str.compare(str_property_types[i], Qt::CaseInsensitive) == 0)
        {
            return (property_type_t)i;
        }
    }

    return nr_device_property::Invalid;
}

QString nr_device_property::property_type_to_string(property_type_t type)
{
    const QString str_property_types[] =  {"Invalid",
        "PropertyGroup",
        "Property"};

    for(int i = 0; i < NR_ARRAY_SIZE(str_property_types); ++i)
    {
        if ( type == property_type_t(i) )
        {
            Q_ASSERT( type!=property_type_t(0) );
            return str_property_types[i];
        }
    }

    Q_ASSERT(0);
    return "";
}

QString nr_device_property::property_type_to_string()
{
    return nr_device_property::property_type_to_string(this->property_type());
}

void nr_device_property::copy_data(nr_device_property& prop)
{
    // Copy each field
    this->id() = prop.id();
    this->name() = prop.name();
    this->value() = prop.value();
    this->property_type() = prop.property_type();

    // Copy children
    for(nr_device_property_hash::iterator it = prop.children().begin();
        it != prop.children().end(); ++it)
    {
        nr_device_property* child_prop = new nr_device_property(*it.value());
        this->children().insert(it.key(), child_prop);
    }

}

void nr_device_property::serialize(QDataStream& data_stream)
{
    // Save each field
    data_stream << this->id();
    data_stream << this->name();
    data_stream << this->value();
    data_stream << (int)this->property_type();

    // Save the count
    data_stream << this->children().count();
    
    // Copy children
    for(nr_device_property_hash::iterator it = this->children().begin();
        it != this->children().end(); ++it)
    {
        data_stream << it.key();
        nr_device_property* child_prop = it.value();
        child_prop->serialize(data_stream);
    }
    
    nr_object::serialize(data_stream);
}

void nr_device_property::deserialize(QDataStream& data_stream)
{
    // Get each field
    data_stream >> this->id();
    data_stream >> this->name();
    data_stream >> this->value();

    int prop_type;
    data_stream >> prop_type;
    this->property_type() = (nr_device_property::property_type_t)prop_type;;

    // Get the count
    int child_count = 0;
    data_stream >> child_count;
    
    // Get children
    for(int i = 0; i < child_count; ++i)
    {
        nr_id_t id;
        nr_device_property* prop = new nr_device_property;

        data_stream >> id;
        prop->deserialize(data_stream);
        
        this->children().insert(id, prop);
    }

    nr_object::deserialize(data_stream);
}